﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Actions.Parameters
{
    public class BoolParameter : IParameter, IXmlBuildable
    {
        XmlNode innerXmlNode;

        public bool IsChildOfUrl {get;set;}
        public bool Value { get; set; }
        public string Name { get; set; }

        public BoolParameter(bool childOfUrl)
        {
            this.IsChildOfUrl = childOfUrl;
        }

        public BoolParameter(XmlNode node, bool childOfUrl)
            : this(childOfUrl)
        {
            try
            {
                this.innerXmlNode = node;

                if (XmlHelper.GetAttribute(node, "Value").Length > 0)
                {
                    this.Value = XmlHelper.GetAttribute(node, "Value") == "true";
                }

                if (XmlHelper.GetAttribute(node, "Name").Length > 0)
                {
                    this.Name = XmlHelper.GetAttribute(node, "Name");
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParameter Members

        public void AddParameterNode(XmlNode parentNode)
        {
            try
            {
                XmlNode parameterNode = XmlHelper.AddNode(parentNode, "BoolParameter");
                XmlHelper.AddAttribute(parameterNode, "Value", this.Value);

                if (this.IsChildOfUrl)
                {
                    XmlHelper.AddAttribute(parameterNode, "Name", this.Name);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IXmlBuildable Members

        public void AddNode(XmlNode parentNode)
        {
            try
            {
                this.AddParameterNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public XmlNode GetXmlNode()
        {
            return this.innerXmlNode;
        }

        public void RemoveChildNode(XmlNode node)
        {
            throw new Exception("This item does not have child element!");
        }

        #endregion

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode boolParamNode = new TreeNode("BoolParameter");
                boolParamNode.ContextMenuStrip = ContextMenuStripHelper.BuildStandardContextMenuStrip(boolParamNode);
                boolParamNode.Tag = this;

                parentNode.Nodes.Add(boolParamNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
